<?php
if(!isset($dbselect)) //前面没设置 数据库 则指定使用 DB3
    $dbselect   = Db::G3();
//总的统计
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and repay_time>'$startdate' and  repay_time<'$enddate'";  
} 
$rows = $dbselect->GetRows("SELECT cid,repay_time as '代收时间', SUM(repay_account) AS '代收总额', COUNT(repay_account) AS '次数',
SUM(repay_interest) AS '利息', sum(repay_capital) AS '本金',
SUM( repay_sf ) AS '其他费用'
FROM 05b_2list_repay
WHERE  1 $repaywhere
GROUP BY cid, repay_time ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = strtotime($row['代收时间']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $r_a    = $row['代收总额'];
    $r_c    = $row['本金'];
    $r_i    = $row['利息'];
    $count  = $row['次数'];
    if(($r_c+$r_i)>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,'mon_moneyyhall',($r_c+$r_i),$Y,$M,$D); 
    }
}

//正常还款的
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>='$startdate' and  repay_yestime<='$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,repay_yestime as '还款时间', SUM(repay_account_yes) AS '还款总额', COUNT(repay_account_yes) AS '次数',
SUM(repay_interest_yes) AS '还款利息', sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',SUM(late_interest) AS '逾期罚息',
sum(if(late_interest>0,1,0)) AS '罚息次数',SUM(late_reminder) AS '逾期催收费', sum(if(late_reminder>0,1,0)) AS '逾期催收费' 
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=0 and repay_web=0 $repaywhere
GROUP BY cid, repay_yestime ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = strtotime($row['还款时间']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $r_li   = $row['逾期罚息'];
    $r_lr   = $row['逾期催收费'];
    $count  = $row['次数'];
    $c_li   = $row['罚息次数'];
    $c_lr   = $row['逾期催收费'];

    if($r_a>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_account',$r_a,$Y,$M,$D);//总的还款费用
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_account",$r_a,$Y,$M,$D); //分站统计 
    }
    if($r_c>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_capital',$r_c,$Y,$M,$D);//总的还款本金
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_capital",$r_c,$Y,$M,$D); //分站统计   
    }
       
    if($r_i>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_interest',$r_i,$Y,$M,$D);//总的还款利息
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_interest",$r_i,$Y,$M,$D); //分站统计  
    }

    if($r_li>0){
        CTLog::G()->doctAndDayReal($c_li.",rp_{$Y}{$m}_st01",1,'tc_yq_faxi',$r_li,$Y,$M,$D);
        CTLog::G()->doctAndDayReal($c_li.",rp_{$Y}{$m}_cid",$cid,"tc_yq_faxi",$r_li,$Y,$M,$D); //分站统计 
    }
    if($r_lr>0){
        CTLog::G()->doctAndDayReal($c_lr.",rp_{$Y}{$m}_st01",1,'tc_yq_csf',$r_lr,$Y,$M,$D);
        CTLog::G()->doctAndDayReal($c_lr.",rp_{$Y}{$m}_cid",$cid,"tc_yq_csf",$r_lr,$Y,$M,$D); //分站统计
    }
}

//垫付还款
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>'$startdate' and  repay_yestime<'$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,repay_yestime as '垫付时间', SUM(repay_account_yes) AS '还款总额', COUNT(repay_account_yes) AS '次数',
SUM(repay_interest_yes) AS '还款利息',sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',SUM(late_interest) AS '逾期罚息',
sum(if(late_interest>0,1,0)) AS '罚息次数',SUM(late_forfeit) AS '滞纳金', sum(if(late_forfeit>0,1,0)) AS '滞纳金次数' ,sum(dianfu_money) as '垫付金额',
sum(dianfu_capital) as '垫付本金',sum(dianfu_sf) as '垫付收费'  
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 $repaywhere
GROUP BY cid, repay_yestime ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = strtotime($row['垫付时间']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $count  = $row['次数'];
    if($r_a>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_account',$r_a,$Y,$M,$D);//总的还款费用
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_account",$r_a,$Y,$M,$D); //分站统计
    }
    if($r_c>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_capital',$r_c,$Y,$M,$D);//总的还款本金
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_capital",$r_c,$Y,$M,$D); //分站统计   
    }
    if($r_i>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_interest',$r_i,$Y,$M,$D);//总的还款利息
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_interest",$r_i,$Y,$M,$D); //分站统计  
    }
    
    if($row['垫付金额']>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tot_df_account',$row['垫付金额'],$Y,$M,$D);  // 网站统计信息 垫付总额
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_df_account",$row['垫付金额'],$Y,$M,$D); //分站统计   垫付总额
    }
    if($row['垫付本金']>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tot_df_capital',$row['垫付本金'],$Y,$M,$D);// 垫付本金[垫付的利息可以通过减法计算出来，这里就不重复统计了]
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_df_capital",$row['垫付本金'],$Y,$M,$D); //分站统计   垫付总额
    }
    if($row['垫付收费']>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tot_df_other',$row['垫付收费'],$Y,$M,$D);   // 垫付其他手续费  
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_df_other",$row['垫付收费'],$Y,$M,$D); //分站统计   垫付总额
    }
}

//垫资还款
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_yestime>'$startdate' and  repay_yestime<'$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,repay_yestime as '垫资时间', SUM(repay_account_yes) AS '还款总额', COUNT( repay_account_yes ) AS '次数',
SUM(repay_interest_yes) AS '还款利息',sum(repay_capital_yes) AS '还款本金',SUM(repay_sf) AS '收取的费用',sum(dianzi_money) as '垫资金额',SUM(late_interest) AS '逾期罚息',
sum(if(late_interest>0,1,0)) AS '罚息次数',SUM(late_reminder) AS '逾期催收费', sum(if(late_reminder>0,1,0)) AS '逾期催收费' 
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 $repaywhere
GROUP BY cid, repay_yestime ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = strtotime($row['垫资时间']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $r_a    = $row['还款总额'];
    $r_c    = $row['还款本金'];
    $r_i    = $row['还款利息'];
    $r_li   = $row['逾期罚息'];
    $r_lr   = $row['逾期催收费'];
    $r_dz   = $row['垫资金额'];
    $count  = $row['次数'];
    $c_li   = $row['罚息次数'];
    $c_lr   = $row['逾期催收费'];

    if($r_a*1<>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_account',$r_a,$Y,$M,$D);//总的还款费用
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_account",$r_a,$Y,$M,$D); //分站统计    
    }
    if($r_c*1<>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_capital',$r_c,$Y,$M,$D);//总的还款本金
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_capital",$r_c,$Y,$M,$D); //分站统计   
    }
    if($r_i*1<>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tc_interest',$r_i,$Y,$M,$D);//总的还款利息
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tc_interest",$r_i,$Y,$M,$D); //分站统计  
    }
    if($r_dz*1<>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tot_dianzhi',$r_dz,$Y,$M,$D);//总的还款利息
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_dianzhi",$r_dz,$Y,$M,$D); //分站统计
    }  
    
    /*if($r_li>0){
        CTLog::G()->doctAndDayReal($c_li.",rp_{$Y}{$m}_st01",,'tc_yq_faxi',$r_li,$Y,$M,$D);
        CTLog::G()->doctAndDayCidReal($c_li.",rp_{$Y}{$m}_cid",$cid,"tc_yq_faxi",$r_li,$Y,$M,$D); //分站统计 
    }*/
    /*if($r_lr>0){
        CTLog::G()->doctAndDayReal($c_lr.",rp_{$Y}{$m}_st01",,'tc_yq_csf',$r_lr,$Y,$M,$D);
        CTLog::G()->doctAndDayCid($c_lr.",rp_{$Y}{$m}_cid",$cid,"tc_yq_csf",$r_lr,$Y,$M,$D); //分站统计
    }*/
}

//垫付还款收回
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
  $repaywhere    = " and  repay_backtime>'$startdate' and  repay_backtime<'$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,SUBSTR( repay_backtime, 1, 10 ) as '收回垫付款',sum(dianfu_money) as '垫付金额',COUNT(dianfu_money) AS '次数',
sum(if(csf>0,csf,0)) as '催收费',sum(dianfu_money+csf) as '收回垫付金额',sum(if(csf>=0,0,-csf)) as '垫付损失',
sum(if(csf>0,1,0)) as '催收费次数',sum(if(csf<0,1,0)) as '损失次数'
FROM 05b_2list_repay
WHERE repay_status=1 and repay_web=1 and repay_backstate=1 $repaywhere
GROUP BY cid, SUBSTR( repay_backtime, 1, 10 ) ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = strtotime($row['收回垫付款']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $count  = $row['次数'];
    $Money  = $row['收回垫付金额'];
    $CS     = $row['催收费'];
    $c_CS   = $row['催收费次数'];
    $SS     = $row['垫付损失'];
    $c_SS   = $row['损失次数'];
    if($count>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tot_dianfu_repay',$Money,$Y,$M,$D);         //总站垫付收回总额
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_dianfu_repay",$Money,$Y,$M,$D);    //分站垫付收回统计
    }
    if($c_SS>0){
        CTLog::G()->doctAndDayReal($c_SS.",rp_{$Y}{$m}_st01",1,'tot_dianfu_ss',$SS,$Y,$M,$D);                //总站垫付损失
        CTLog::G()->doctAndDayReal($c_SS.",rp_{$Y}{$m}_cid",$cid,"tot_dianfu_ss",$SS,$Y,$M,$D);           //分站垫付收损失
    }
    if($c_CS>0){
        CTLog::G()->doctAndDayReal($c_CS.",rp_{$Y}{$m}_st01",1,'tot_dianfu_csf',$CS,$Y,$M,$D);               //总站垫付崔收费
        CTLog::G()->doctAndDayReal($c_CS.",rp_{$Y}{$m}_cid",$cid,"tot_dianfu_csf",$CS,$Y,$M,$D);          //分站垫付崔收费
    }
}



//垫资还款收回
$repaywhere      = "";
if($startdate!="" && $enddate!=""){
    $repaywhere    = " and  repay_backtime>'$startdate' and  repay_backtime<'$enddate'";  
}

$rows = $dbselect->GetRows("SELECT cid,SUBSTR( repay_backtime, 1, 10 ) as '收回垫资款',sum(dianzi_money) as '垫资金额',COUNT(repay_account_yes) AS '次数',
sum(if(csf>0,csf,0)) as '催收费',sum(dianzi_money+csf) as '收回垫资金额',sum(if(csf>=0,0,-csf)) as '垫资损失',
sum(if(csf>0,1,0)) as '催收费次数',sum(if(csf<0,1,0)) as '损失次数'
FROM 05b_2list_repay
WHERE repay_status=1 and isdianzi=1 and repay_backstate=1 $repaywhere
GROUP BY cid, SUBSTR( repay_backtime, 1, 10 ) ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $ctime  = strtotime($row['收回垫资款']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime);
    $count  = $row['次数'];
    $Money  = $row['收回垫资金额'];
    $CS     = $row['催收费'];
    $c_CS   = $row['催收费次数'];
    $SS     = $row['垫资损失'];
    $c_SS   = $row['损失次数'];
    if($count>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,'tot_dianzhi_repay',$Money,$Y,$M,$D);         //总站垫资收回总额
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_dianzhi_repay",$Money,$Y,$M,$D);    //分站垫资收回统计
    }
    if($c_SS>0){
        CTLog::G()->doctAndDayReal($c_SS.",rp_{$Y}{$m}_st01",1,'tot_dianzhi_ss',$SS,$Y,$M,$D);                //总站垫资损失
        CTLog::G()->doctAndDayReal($c_SS.",rp_{$Y}{$m}_cid",$cid,"tot_dianzhi_ss",$SS,$Y,$M,$D);           //分站垫资收损失
    }
    if($c_CS>0){
        CTLog::G()->doctAndDayReal($c_CS.",rp_{$Y}{$m}_st01",1,'tot_dianzhi_csf',$CS,$Y,$M,$D);               //总站垫资崔收费
        CTLog::G()->doctAndDayReal($c_CS.",rp_{$Y}{$m}_cid",$cid,"tot_dianzhi_csf",$CS,$Y,$M,$D);          //分站垫资崔收费
    }
}

///完成